import sys,math,os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Drawing(QWidget):
    def __init__(self,parent=None):
        super(Drawing,self).__init__(parent)

        textCopyButton=QPushButton("Copy Text")
        textPasteButton=QPushButton("Paste Text")
        HtmlCopyButton=QPushButton("Copy Html")
        HtmlPasteButton=QPushButton("Paste Html")
        ImageCopyButton=QPushButton("Copy Image")
        ImagePasteButton=QPushButton("Paste Image")
        self.textLabel=QLabel("Original text")
        self.imageLabel=QLabel()
        #self.imageLabel.setPixmap(QPixmap(os.path.join(os.path.dirname(__file__),"panda.jpg")))
        layout=QGridLayout()
        layout.addWidget(textCopyButton,0,0)
        layout.addWidget(ImageCopyButton,0,1)
        layout.addWidget(HtmlCopyButton,0,2)
        layout.addWidget(textPasteButton,1,0)
        layout.addWidget(ImagePasteButton,1,1)
        layout.addWidget(HtmlPasteButton,1,2)
        layout.addWidget(self.textLabel,2,0,1,2)
        layout.addWidget(self.imageLabel,2,2)
        self.setLayout(layout)
        textCopyButton.clicked.connect(self.copyText)
        textPasteButton.clicked.connect(self.pasteText)
        HtmlCopyButton.clicked.connect(self.copyHtml)
        HtmlPasteButton.clicked.connect(self.pasteHtml)
        ImageCopyButton.clicked.connect(self.copyImage)
        ImagePasteButton.clicked.connect(self.pasteImage)
        self.setWindowTitle("Clipboard例子")
    def copyText(self):
        clipboard=QApplication.clipboard()
        clipboard.setText("剪贴板")
    def pasteText(self):
        clipboard = QApplication.clipboard()
        self.textLabel.setText(clipboard.text())
    def copyHtml(self):
        mimeData=QMimeData()
        mimeData.setHtml("<b>bold and<font color=red>Red</font></b>")
        clipboard = QApplication.clipboard()
        clipboard.setMimeData(mimeData)
    def pasteHtml(self):
        clipboard = QApplication.clipboard()
        mimeData=clipboard.mimeData()
        if mimeData.hasHtml():
            self.textLabel.setText(mimeData.html())

    def copyImage(self):
        clipboard = QApplication.clipboard()
        clipboard.setPixmap(QPixmap(os.path.join(os.path.dirname(__file__),"panda.jpg")))


    def pasteImage(self):
        clipboard = QApplication.clipboard()

        self.imageLabel.setPixmap(clipboard.pixmap())


if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=Drawing()
    demo.show()
    sys.exit(app.exec_())